package cc.vv.baselibrary.util;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import cc.vv.baselibrary.R;
import cc.vv.lkbasecomponent.base.app.LKBaseApplication;
import cc.vv.lkbasecomponent.util.LKScreenUtil;

/**
 * 高效率显示Toast的方法，不需要等待已经弹出的Toast消失，即可显示新的Toast
 *
 * @author nanPengFei
 */
public class LKToastUtil {
    public static Toast mToast;
    @SuppressLint("StaticFieldLeak")
    private static TextView tv_toastMsg;

    /**
     * 快速连续弹Toast
     *
     * @param msg 需要显示的内容
     */
    public static void showToastLong(final String msg) {
        LKBaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                if (null == mToast || null == tv_toastMsg) {
                    mToast = Toast.makeText(LKBaseApplication.getApplication(), "", Toast.LENGTH_LONG);
                    @SuppressLint("InflateParams") View view = LayoutInflater.from(LKBaseApplication.getApplication()).inflate(R.layout.view_toast, null);
                    tv_toastMsg = view.findViewById(R.id.tv_toastMsg);
                    mToast.setView(view);
                    mToast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, LKScreenUtil.dp2px(150));
                }
                tv_toastMsg.setText(msg);
                mToast.show();
            }
        });
    }

    /**
     * 快速连续弹Toast
     *
     * @param msg 需要显示的内容
     */
    public static void showToastShort(final String msg) {
        LKBaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(LKBaseApplication.getApplication(), "", Toast.LENGTH_SHORT);
                    @SuppressLint("InflateParams") View view = LayoutInflater.from(LKBaseApplication.getApplication()).inflate(R.layout.view_toast, null);
                    tv_toastMsg = view.findViewById(R.id.tv_toastMsg);
                    mToast.setView(view);
                    mToast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, LKScreenUtil.dp2px(150));
                }
                tv_toastMsg.setText(msg);
                mToast.show();
            }
        });
    }
}
